package ywh.player.media
{
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import org.osmf.containers.MediaContainer;
	import org.osmf.elements.ImageElement;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaFactory;
	import org.osmf.media.MediaPlayer;
	import org.osmf.media.MediaPlayerSprite;
	import org.osmf.media.MediaPlayerState;
	import org.osmf.media.URLResource;
	import org.osmf.utils.OSMFSettings;
	import ywh.list.CourseItem;
	import ywh.manager.Core;
	import ywh.manager.Tag;
	import ywh.player.AbstractPlayer;
	import ywh.player.PlayerState;
	
	/**
	 * flv f4v 播放器, 支持http:// rtsp://
	 * @author 杨伟华
	 * @version 2.0
	 *
	 * Created 1/16/2013 3:47 AM
	 */
	public class OSMFPlayer extends AbstractPlayer
	{
		protected var _mediasprite:MediaPlayerSprite;
		protected var _mediaplayer:MediaPlayer;
		protected var _mediacontainer:MediaContainer;
		
		public function OSMFPlayer()
		{
			super();
			OSMFSettings.enableStageVideo = true;
		}
		
		override protected function initDisplayer():void
		{
			render = _mediasprite = new MediaPlayerSprite();
			_mediaplayer = _mediasprite.mediaPlayer;
			_mediaplayer.autoPlay = false;
		}
		
		override public function set playItem(value:CourseItem):void
		{
			super.playItem = value;
			var isAbsolutePath:Boolean = _playItem.getPropBool(Tag._IS_ABSOLUTE_PATH);
			var urlString:String;
			urlString = _playItem.getPropString(Tag._PARH) + _playItem.getPropString(Tag._URL);
			if (!isAbsolutePath)
			{
				urlString = Core.getResolvePath(urlString);
			}
			
			var urlresource:URLResource = new URLResource(urlString);
			var mediaType:String = _playItem.getPropString(Tag._OSMF_MEDIA_TYPE);
			mediaType && (urlresource.mediaType = mediaType);
			var mimeType:String = _playItem.getPropString(Tag._OSMF_MIME_TYPE);
			mimeType && (urlresource.mimeType = mimeType);
			_mediasprite.resource = urlresource;
			_mediasprite.media.hasOwnProperty("smoothing") && (_mediasprite.media["smoothing"] = true);
		}
		
		override protected function canSeekTo(value:Number):Boolean
		{
			return _mediaplayer.canSeekTo(value);
		}
		
		override protected function get _pState():String
		{
			var stateString:String;
			switch (_mediaplayer.state)
			{
				case MediaPlayerState.BUFFERING: 
				case MediaPlayerState.LOADING: 
				case MediaPlayerState.UNINITIALIZED: 
					stateString = PlayerState.LOADING;
					break;
				case MediaPlayerState.PAUSED: 
					stateString = PlayerState.PAUSED;
					break;
				case MediaPlayerState.PLAYING: 
					stateString = PlayerState.PLAYING;
					break;
				case MediaPlayerState.READY: 
					stateString = PlayerState.READY;
					break;
				case MediaPlayerState.PLAYBACK_ERROR: 
					stateString = PlayerState.PLAYBACK_ERROR;
					break;
			}
			return stateString;
		
		}
		
		override protected function _pPause():void
		{
			_mediaplayer.pause();
		}
		
		override protected function _pPlay():void
		{
			_mediaplayer.play();
		}
		
		override protected function get _pTime():Number
		{
			return _mediaplayer.currentTime;
		}
		
		public function get mediaplayer():MediaPlayer
		{
			return _mediaplayer;
		}
		
		override protected function _pSeekTo(value:Number):void
		{
			_mediaplayer.play();
			_mediaplayer.seek(value);
		}
	}

}